// File Name: usart_task.c
// Author: kukukukuku~
// Date: 2024-12-21

#include "./Key/inc/key_var.h"
#include "./OLED/inc/oled_var.h"
#include "./RS485/inc/rs485_var.h"
#include "./USART/inc/usart_task.h"

//USART任务句柄
TaskHandle_t usartTaskHandle;


/**
 * @brief   USART任务
 * @param   arg:    任务参数
 * @retval  None
 * @note    None
*/
void usart_task(void *arg)
{
    usart_object_init(&usartObj, usartTxBuffer, usartRxBuffer);
    
    while(1)
    {
        //USART接收到数据，转发到RS485
        if(usart_rx_finish_flag_get(&usartObj) == USART_RX_STATE_COMPLETE)
        {
            usart_received_data_handle(&usartObj);
            rs485_send_data(&rs485Obj, usart_rx_buffer_get(&usartObj), usart_rx_data_len_get(&usartObj));
            ++usartToRs485Cnt;
            
            usart_rx_buffer_clear(&usartObj);
            usart_rx_data_len_clear(&usartObj);
            usart_rx_finish_flag_clear(&usartObj);
            
            oledRefreshFlag = 1;
        }
        
        //长按清除按键，打印串口测试
        if(key_is_long_press_triged(&keyClearObj))
        {
            usart_send_data(&usartObj, (uint8_t *)"USART test ok!\r\n", sizeof("USART test ok!\r\n") - 1);
        }
        
        //清除串口转发到RS485的计数
        if(key_is_click_triged(&keyClearObj))
        {
            rs485ToUsartCnt = 0;
            usartToRs485Cnt = 0;
            oledRefreshFlag = 1;
            key_clear_click_handle(&keyClearObj);
        }
        
        vTaskDelay(5);
    }
}
